<?php
namespace app\admin\controller;
use app\common\model\ProjectClassify as ProjectClassifyModel;

use app\common\controller\AdminBase;
use think\Config;
use think\Db;
use think\Session;
/**
 * 预约项目分类管理
 * Class ProjectClassify
 * @package app\admin\controller
 */
class ProjectClassify extends AdminBase
{

    protected $project_classify_model;

    protected function _initialize()
    {
        parent::_initialize();
        $this->project_classify_model = new ProjectClassifyModel();


        $project_classify_list       = $this->project_classify_model->order(['sort' => 'DESC', 'id' => 'ASC'])->select();
        $project_classify_level_list = array2level($project_classify_list);
        $this->assign('project_classify_level_list', $project_classify_level_list);
    }

    /**
     * 预约项目分类管理
     * @return mixed
     */
    public function index()
    {
        return $this->fetch();
    }

    /**
     * 添加预约项目分类
     * @param string $pid
     * @return mixed
     */
    public function add($pid = '')
    {
        return $this->fetch('add', ['pid' => $pid]);
    }

    /**
     * 保存预约项目分类
     */
    public function save()
    {
        if ($this->request->isPost()) {
            $data            = $this->request->post();
            $validate_result = $this->validate($data, 'ProjectClassify');

            if ($validate_result !== true) {
                $this->error($validate_result);
            } else {
                if ($this->project_classify_model->allowField(true)->save($data)) {
                    $this->success('保存成功');
                } else {
                    $this->error('保存失败');
                }
            }
        }
    }

    /**
     * 编辑预约项目分类
     * @param $id
     * @return mixed
     */
    public function edit($id)
    {
        $project_classify = $this->project_classify_model->find($id);

        return $this->fetch('edit', ['project_classify' => $project_classify]);
    }

    /**
     * 更新预约项目分类
     * @param $id
     */
    public function update($id,$status)
    {
        if ($this->request->isPost()) {
            $data            = $this->request->post();
            $validate_result = $this->validate($data, 'ProjectClassify');

            if ($validate_result !== true) {
                $this->error($validate_result);
            } else {
                if ($this->project_classify_model->allowField(true)->save($data, $id) !== false) {
                    $this->success('更新成功');
                } else {
                    $this->error('更新失败');
                }
            }
        }else{
            $auth_group              = $this->project_classify_model->find($id);
            $auth_group->id          = $id;
            $auth_group->status      = $status;
            if ($auth_group->save() !== false) {
                $this->success('状态更新成功');
            } else {
                $this->error('状态更新失败');
            }
        }
    }

    /**
     * 删除预约项目分类
     * @param $id
     */
    public function delete($id)
    {
        $sub_menu = $this->project_classify_model->where(['pid' => $id])->find();
        if (!empty($sub_menu)) {
            $this->error('此预约项目分类下存在子预约项目分类，不可删除');
        }
        if ($this->project_classify_model->destroy($id)) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }

}